﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using RoofRunner.GameComponents;
using RoofRunner.GameComponents.Rendering;

namespace RoofRunner.Actors.ActorComponents.Rendering
{
    public class ModelRenderComponent : RenderComponent
    {
        #region Properties
        public virtual Model Model { get; set; }
        public String ModelRessourceName { get; set; }
        #endregion // Properties


        #region Constructors
        public ModelRenderComponent(String modelRessourceName) : base()
        {
            if (modelRessourceName == null) throw new ArgumentNullException("ressourceName");
            this.ModelRessourceName = modelRessourceName;
        }


        public ModelRenderComponent(String modelRessourceName, String textureRessourceName)
            : base(textureRessourceName)
        {
            if (modelRessourceName == null) throw new ArgumentNullException("ressourceName");
            this.ModelRessourceName = modelRessourceName;
        }

        public ModelRenderComponent(String modelRessourceName, String textureRessourceName, Color color) : base(textureRessourceName, color)
        {
            if (modelRessourceName == null) throw new ArgumentNullException("ressourceName");
            this.ModelRessourceName = modelRessourceName;
        }
        #endregion // Constructors


        #region Methods
        public override void LoadContent(ContentManager content)
        {
            if(this.ModelRessourceName != null)
                this.Model = content.Load<Model>(this.ModelRessourceName);

            foreach (var mm in this.Model.Meshes)
            {
                foreach (Effect e in mm.Effects)
                {
                    IEffectLights iel = e as IEffectLights;
                    if (iel != null)
                        iel.EnableDefaultLighting();
                }
            }
            base.LoadContent(content);
        }
        

        public override void Render(GameTime gameTime, RenderWorld renderWorld)
        {
            if (this.Model != null)
                renderWorld.RenderModel(this);
            base.Render(gameTime, renderWorld);
        }
        #endregion // Methods
    }
}
